#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <errno.h>

extern char** environ;

int main()
{
	pid_t childId = fork(); 
	if(childId<0)
	{
		//申请失败
		printf("fork::%s",strerror(errno));
		exit(1);
	}
	else if(childId>0)
	{
		//父进程
		int cur=5;
		while(cur--)
		{
			printf("parent: pid=%d : ppid=%d \n",getpid(),getppid());
			sleep(1);
		}

		//pid_t rid = wait(NULL);

		int status=0;
		pid_t rid = waitpid(childId, &status, 0);
		
		if (rid>0)
		{
			if(WIFEXITED(status))
			{
				printf("wait success rid: %d| exit code: %d| exit signal: %d\n", rid, WEXITSTATUS(status), status&0x7F);
			}
			else
			{
				printf("exit error\n");
			}
			//printf("wait success rid: %d| exit code: %d| exit signal: %d\n", rid, (status>>8)&0xFF, status&0x7F);
		}
		else
		{
			printf("wait failed rid: %d: %s\n",rid,strerror(errno));
		}
	}
	else
	{
		//子进程
		int cur=5;
		while(cur--)
		{
			printf("child : pid=%d : ppid=%d \n",getpid(),getppid());
			sleep(1);
		}
		exit(33);
	}	

	return 0;
}
